//
//  SuperviseTaskIssueDetailView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/9.
//

import Foundation
import UIKit

class SuperviseTaskIssueDetailView: UIScrollView {
    let basicV = UIView.createBase()
    let nameL = UILabel()
    let resultItem = InspListDetailItem(withKeyWidth: 100)    // 检查结论
    let personItem = InspListDetailItem(withKeyWidth: 100)  // 检查人
    let dateItem = InspListDetailItem(withKeyWidth: 100)  // 检查时间

    let recordBV = UIView.createBase()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .bg
        showsVerticalScrollIndicator = false
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reload(withModal modal: SpRwbgtzModal?) {
        nameL.text = modal?.rwmc
        resultItem.update(withKey: "检查结论", value: modal?.rwztmc)
        resultItem.valueL.textColor =  modal?.rwztmc == "不合格" ? .hex("#FF3203") : . hex("#66BC60")
        personItem.update(withKey: "检查人", value: modal?.jcrmc)
        dateItem.update(withKey: "检查时间", value: modal?.tjsj)
        updateRecordView(withRecords: modal?.zgjls ?? [])
    }
    
    @objc func recordTapped(_ sender: STaskIssueRecordItem) {
        let modal = sender.modal as! SpRwbgtzZgjlModal
        getFirstViewController()?.navigationController?.pushViewController(SuperviseTaskRecordItemVC(modal: modal), animated: true)
    }
    
    func updateRecordView(withRecords records: [SpRwbgtzZgjlModal]) {
        recordBV.removeAllSubViews()
        
        let recordTV = TitleItemView(withTitle: "整改记录", hasIcon: false)
        recordBV.addSubview(recordTV)
        recordTV.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(46)
        }
        
        let line = UIView()
        line.backgroundColor = .hex("#ECECEC")
        recordBV.addSubview(line)
        line.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(recordTV.snp.bottom)
            make.left.equalToSuperview().offset(13)
            make.right.equalToSuperview().offset(-13)
            make.height.equalTo(0.5)
        }

        var lastI: STaskIssueRecordItem?
        for (index, record) in records.enumerated() {
            let itemV = STaskIssueRecordItem()
            itemV.update(withModal: record, isLast: index == records.count - 1)
            itemV.addTarget(self, action: #selector(recordTapped), for: .touchUpInside)
            recordBV.addSubview(itemV)
            itemV.snp.makeConstraints { make in
                make.left.right.equalToSuperview()
                make.height.equalTo(80)
                if lastI == nil {
                    make.top.equalTo(line.snp.bottom).offset(16)
                } else {
                    make.top.equalTo(lastI!.snp.bottom)
                }
                if index == records.count - 1 {
                    make.bottom.equalToSuperview()
                }
            }
            lastI = itemV
        }
    }
    
    func setupBasicUI() {
        addSubview(basicV)
        basicV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        nameL.numberOfLines = 0
        nameL.font = .systemFont(ofSize: 15)
        basicV.addSubview(nameL)
        nameL.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.top.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
        }
        
        let line = UIView()
        line.backgroundColor = .hex("#EFEFEF")
        basicV.addSubview(line)
        line.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(nameL.snp.bottom).offset(26)
            make.left.equalTo(nameL.snp.left)
            make.right.equalTo(nameL.snp.right)
            make.height.equalTo(0.5)
        }

        basicV.addSubview(resultItem)
        resultItem.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(line.snp.bottom).offset(10)
            make.height.equalTo(40)
        }
        
        basicV.addSubview(personItem)
        personItem.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(resultItem.snp.bottom)
            make.height.equalTo(40)
        }
        
        basicV.addSubview(dateItem)
        dateItem.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(personItem.snp.bottom)
            make.height.equalTo(40)
            make.bottom.equalToSuperview().offset(-12)
        }
    }
    
    func setupUI() {
        // 基本信息
        setupBasicUI()
        
        // 整改记录
        addSubview(recordBV)
        recordBV.snp.makeConstraints { make in
            make.top.equalTo(basicV.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
            make.bottom.equalTo(self.snp.bottom).offset(-40)
        }
    }
}


class STaskIssueRecordItem: UIControl {
    var modal: Any?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func update(withModal modal: SpRwbgtzZgjlModal?, isLast: Bool) {
        self.modal = modal

        // 任务状态 1 待检查 2 合格 3不合格
        switch modal?.rwzt {
        case "1":
            statusV.title = "整改"
            nameL.text = "问题整改"
            nameL.textColor = .black
            break
        case "2":
            statusV.title = "检查"
            nameL.text = "合格"
            nameL.textColor = .hex("#66BC60")
            break
        case "3":
            statusV.title = "检查"
            nameL.text = "不合格"
            nameL.textColor = .hex("#FF3203")
            break
        default:
            statusV.title = "检查"
            nameL.text = "合格"
            nameL.textColor = .hex("#66BC60")
            break
        }
        dateL.text = "\(modal?.zgrmc ?? "")（\(modal?.tjsj ?? "")）"

        upArrowIV.isHidden = isLast
    }
    
    // MARK: - Setup
    func setupUI() {
        addSubview(statusV)
        statusV.snp.makeConstraints { make in
            make.width.height.equalTo(41)
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(20)
        }
        
        addSubview(nameL)
        nameL.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(2)
            make.left.equalTo(statusV.snp.right).offset(10)
            make.right.equalToSuperview().offset(-20)
        }
        
        addSubview(dateL)
        dateL.snp.makeConstraints { make in
            make.top.equalTo(nameL.snp.bottom).offset(10)
            make.left.equalTo(nameL.snp.left)
        }
        
        addSubview(upArrowIV)
        upArrowIV.snp.makeConstraints { make in
            make.centerX.equalTo(statusV.snp.centerX)
            make.top.equalTo(statusV.snp.bottom).offset(7)
        }
        
        addSubview(arrowIV)
        arrowIV.snp.makeConstraints { make in
            make.centerY.equalTo(statusV.snp.centerY)
            make.right.equalToSuperview().offset(-15)
        }
    }
    
    lazy var statusV: RepairRecordStatusView = {
        let v = RepairRecordStatusView()
        v.status = .finished
        return v
    }()
    
    lazy var nameL: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 15)
        return lab
    }()
    
    lazy var dateL: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 13)
        lab.textColor = .hex("#9E9E9E")
        return lab
    }()
    
    lazy var arrowIV: UIImageView = {
        let iv = UIImageView(image: UIImage.init(systemName: "chevron.right"))
        iv.contentMode = .scaleAspectFit
        iv.tintColor = .hex("#A5A5A5")
        return iv
    }()
    
    lazy var upArrowIV: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "ic_repair_toparrow"))
        iv.contentMode = .scaleAspectFit
        return iv
    }()
}

